const { promisify } = require("util");
const figlet = promisify(require("figlet"));

const clear = require("clear");
const chalk = require("chalk");
const log = (content) => console.log(chalk.redBright(content));
const { clone } = require("./download");
const open = require("open");

module.exports = async (name) => {
  // 打印欢迎界面
  clear();
  const data = await figlet("zxc welcome");
  log(data);
  //克隆项目
  await clone("github:su37josephxia/vue-template", name);

  //安装依赖
  console.log("安装依赖");
  await spawn("npm", ["install"], { cwd: `./${name}` });

  log(
    `
    =========================================

                  安装完成

    =========================================s
    `
  );

  //打开浏览器
  open("http://localhost:8080");
  await spawn("npm", ["run", "serve"], { cwd: `./${name}` });
};
function spawn(...args) {
  const { spawn } = require("child_process");
  return new Promise((resolve) => {
    const proc = spawn(...args);
    proc.stdout.pipe(process.stdout);
    proc.stderr.pipe(process.stderr);
    proc.on("close", () => {
      resolve();
    });
  });
}
